package com.luckku.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luckku.boot.common.api.ApiResult;
import com.luckku.boot.entity.Admin;
import com.luckku.boot.entity.UserExam;
import com.luckku.boot.mapper.UserExamMapper;
import com.luckku.boot.service.ExamSiteService;
import com.luckku.boot.service.UserExamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luckku.boot.utils.MD5Utils;
import com.luckku.boot.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Luckku
 * @since 2022-10-09
 */
@Service
public class UserExamServiceImpl extends ServiceImpl<UserExamMapper, UserExam> implements UserExamService {

    @Autowired
    ExamSiteService examSiteService;

    @Override
    public UserExam executeRegister(UserExam userExam) {
        if (userExam.getUserId() == null && userExam.getExamId() == null && userExam.getSiteId() == null) {
            return userExam;
        }
        QueryWrapper<UserExam> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(UserExam::getUserId, userExam.getUserId())
                .eq(UserExam::getExamId, userExam.getExamId());
        UserExam exist = baseMapper.selectOne(queryWrapper);
        if (exist != null) {
            return userExam;
        }

        SnowFlakeUtil snowFlakeUtil = new SnowFlakeUtil();
        userExam.setAdmissionTicketId(snowFlakeUtil.snowflakeId());
        userExam.setHasPaid("0");
        Integer maxCapacity = examSiteService.getById(userExam.getSiteId()).getMaxCapacity();
        userExam.setSeatNum(new Random().nextInt(maxCapacity));

        baseMapper.insert(userExam);
        return userExam;
    }
}
